<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="专家名称" prop="expertName">
        <el-input
          v-model="queryParams.expertName"
          placeholder="请输入专家名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专家类型" prop="expertType">
        <el-select
          v-model="queryParams.expertType"
          placeholder="请选择专家类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.crm_expert_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否外聘" prop="isExternal">
        <el-select
          v-model="queryParams.isExternal"
          placeholder="请选择是否外聘"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.crm_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="专家介绍" prop="expertRemark">
        <el-input
          v-model="queryParams.expertRemark"
          placeholder="请输入专家介绍"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crm:expert:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['crm:expert:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['crm:expert:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['crm:expert:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="expertList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="专家名称" align="center" prop="expertName" />
      <el-table-column label="专家类型" align="center" prop="expertType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.crm_expert_type"
            :value="scope.row.expertType"
          />
        </template>
      </el-table-column>
      <el-table-column label="是否外聘" align="center" prop="isExternal">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.crm_yes_no"
            :value="scope.row.isExternal"
          />
        </template>
      </el-table-column>
      <el-table-column label="专家级别" align="center" prop="externalGrade">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.crm_external_grade"
            :value="scope.row.externalGrade"
          />
        </template>
      </el-table-column>
      <el-table-column label="专家介绍" align="center" prop="expertRemark" />
      <el-table-column label="证书地址" align="center" prop="certificateUrl">
        <template #default="{ row }">
          <el-button
            type="text"
            @click="readView(row.certificateUrl)"
            v-if="row.certificateUrl"
            >点击查看</el-button
          >
          <label v-else> 未上传 </label>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['crm:expert:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['crm:expert:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改专家库对话框 -->
    <el-drawer
      :title="title"
      direction="rtl"
      :visible.sync="visible"
      size="60%"
      append-to-body
      :wrapperClosable="false"
    >
      <el-card
        class="box-card"
        style="width: 96%; margin-left: 20px; height: 90vh"
      >
        <el-row>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-col :span="12">
              <el-form-item label="是否外聘" prop="isExternal">
                <el-select
                  v-model="form.isExternal"
                  @change="clearUser"
                  placeholder="请选择是否外聘"
                  style="width: 100%"
                >
                  <el-option
                    v-for="dict in dict.type.crm_yes_no"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="form.isExternal === '0'">
              <el-form-item label="专家名称" prop="userId">
                <el-select
                  v-model="form.expertName"
                  placeholder="请选择专家"
                  @change="selectUser"
                  style="width: 100%"
                  filterable
                  clearable
                >
                  <el-option
                    v-for="user in userList"
                    :key="user.userId"
                    :label="user.userName"
                    :value="{ value: user.userId, label: user.userName }"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-else>
              <el-form-item label="专家名称" prop="expertName">
                <el-input
                  v-model="form.expertName"
                  placeholder="请输入专家名称"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="专家类型" prop="expertType">
                <el-select
                  v-model="form.expertType"
                  placeholder="请选择专家类型"
                  style="width: 100%"
                >
                  <el-option
                    v-for="dict in dict.type.crm_expert_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="级别" prop="externalGrade">
                <el-select
                  v-model="form.externalGrade"
                  placeholder="请选择级别"
                  style="width: 100%"
                >
                  <el-option
                    v-for="dict in dict.type.crm_external_grade"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="专家介绍" prop="expertRemark">
                <el-input
                  v-model="form.expertRemark"
                  type="textarea"
                  placeholder="请输入专家介绍"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="expertRemark">
                <el-input
                  v-model="form.remark"
                  type="textarea"
                  placeholder="请输入专家介绍"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="证书地址">
                <file-upload v-model="form.certificateUrl" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item>
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </el-card>
    </el-drawer>
  </div>
</template>

<script>
import {
  listExpert,
  getExpert,
  delExpert,
  addExpert,
  updateExpert,
} from "@/api/crm/expert";
import { getUserListByStatus } from "@/api/system/user";
export default {
  name: "expert",
  dicts: ["crm_expert_type", "crm_yes_no", "crm_external_grade"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专家库表格数据
      expertList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      visible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        expertName: null,
        expertType: null,
        expertRemark: null,
        certificateUrl: null,
      },
      // 表单参数
      form: {
        isExternal: "1",
        expertType: "0",
      },
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "专家名称不能为空", trigger: "change" },
        ],
        expertName: [
          { required: true, message: "专家名称不能为空", trigger: "blur" },
        ],
        expertType: [
          { required: true, message: "专家类型不能为空", trigger: "change" },
        ],
        isExternal: [
          { required: true, message: "是否外聘不能为", trigger: "change" },
        ],
        externalGrade: [
          { required: true, message: "专家级别不能为空", trigger: "change" },
        ],
      },
      userList: [],
    };
  },
  created() {
    this.getList();
    this.getUserListByStatus();
  },
  methods: {
    readView(filePath) {
      window.open(filePath);
    },
    clearUser(){
      this.form.userId = null
      this.form.expertName = null
    },
    selectUser(data){
      const { value, label } = data;
	    this.form.userId = value;
	    this.form.expertName = label;
    },
    getUserListByStatus() {
      getUserListByStatus({ status: "0" }).then((res) => {
        this.userList = res.data;
      });
    },
    /** 查询专家库列表 */
    getList() {
      this.loading = true;
      listExpert(this.queryParams).then((response) => {
        this.expertList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.visible = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        isExternal: "0",
        expertId: null,
        userId: null,
        expertName: null,
        expertType: null,
        expertRemark: null,
        certificateUrl: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.expertId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.visible = true;
      this.title = "添加专家库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const expertId = row.expertId || this.ids;
      getExpert(expertId).then((response) => {
        this.form = response.data;
        this.visible = true;
        this.title = "修改专家库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.expertId != null) {
            updateExpert(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.visible = false;
              this.getList();
            });
          } else {
            addExpert(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.visible = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const expertIds = row.expertId || this.ids;
      this.$modal
        .confirm('是否确认删除专家库编号为"' + expertIds + '"的数据项？')
        .then(function () {
          return delExpert(expertIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "crm/expert/export",
        {
          ...this.queryParams,
        },
        `expert_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
